package com.yy.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.yy.service.ProviderHystrixService;
import com.yy.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author young
 * @date 2022/12/24 20:55
 * @description: 单侧服务降级接口
 */
@Slf4j
@RequestMapping("consumer")
@RestController
public class ConsumerHystrixController {
    @Resource
    private ProviderHystrixService providerHystrixService;
    
    @GetMapping("/test/ok/{id}")
    public Result<String> providerOk(@PathVariable("id") Integer id){
        return providerHystrixService.selectOne(id);
    }
    
   
    //配置服务降级，单侧客户端降级
    @HystrixCommand(fallbackMethod = "down",
    commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",
            value = "1500")
    })
    @GetMapping("/test/wait/{id}")
    public Result<String> providerWait(@PathVariable("id") Integer id){
        return providerHystrixService.selectOneWait(id);
    }
    /**
     * 执行降级后的方法,返回对象Result<String>要一致，否则报错
     * Command type literal pos: unknown; Fallback type literal pos: unknown
     */
    public Result<String> down(@PathVariable("id") Integer id){
        String s ="我消费者端扛不住了，要开始降级处理了,处理id为"+id;
        return Result.ok(s);
    }
}
